//
//  THReadingController.swift
//  TongHuoDemo
//
//  Created by rimi on 2017/5/2.
//  Copyright © 2017年 rimi. All rights reserved.
//

import UIKit

class THReadingController: UIViewController {
    // 滚动标题栏
    var sliderBar: FDSlideBar = FDSlideBar()
    // 内容视图
    var contentView: FDSlideContentView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置滚动标题栏
        setupSlideBar()
        // 设置滚动内容视图
        setupScollContentView()
    }
    
    // 设置状态栏模式
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    // 设置滚动标题栏
    func setupSlideBar() {
        // 设置状态栏背景
        view.backgroundColor = UIColor(white: 0.8, alpha: 1.0)
        let topV = UIView(frame: CGRect(x: 0, y: 0, width: screenW, height: 20))
        topV.backgroundColor = UIColor.black
        view.addSubview(topV)
        
        // 设置滚动标题栏
        sliderBar.backgroundColor = UIColor.black
        sliderBar.itemWidth = screenW/6;
        sliderBar.itemsTitle = ["精选","八卦","视频","日荐","专题","我的关注"]
        sliderBar.itemColor = UIColor.white
        sliderBar.itemSelectedColor = UIColor.orange
        sliderBar.sliderColor = UIColor.orange
        view.addSubview(sliderBar)
        
        // 标题栏的点击事件回调
        sliderBar.slideBarItemSelectedCallback {[weak self] (index) in
            self?.contentView.scroll(to: index)
        }
    }
    
    // 设置滚动内容视图
    func setupScollContentView() {
        let Y = sliderBar.frame.maxY
        contentView = FDSlideContentView(frame: CGRect(x: 0, y:Y , width: screenW, height: screenH - Y))
        contentView.dataSource = self
        view.addSubview(contentView)
        
        contentView.slideContentViewScrollFinished {[weak self] (index) in
            self?.sliderBar.selectItem(at: index)
        }
    }

}

extension THReadingController: FDSlideContentViewDataSource {
    // 内容视图的个数
    func numOfContentView() -> Int {
        return 6
    }
    // 视图内容控制器
    func slideContentView(_ contentView: FDSlideContentView!, viewControllerFor index: UInt) -> UIViewController! {
        switch index {
        case 0:
            let choiceVc = THChoiceController()
            return choiceVc
        case 1:
            let gossipVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "THGossipController")
            return gossipVc
        default:
            let vc = UIViewController()
            let randNum = CGFloat(arc4random_uniform(10))/10.0
            vc.view.backgroundColor = UIColor(white:randNum , alpha: 1.0)
            return vc
        }
    }
}















